package com.fhz.day017;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bawei.lrcutils.YaoLrcHelper;
import com.lauzy.freedom.library.Lrc;
import com.lauzy.freedom.library.LrcView;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private LrcView lrvView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        mediaPlayer = new MediaPlayer();

        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource("http://39.106.204.190:7777/mp3/1405283464.mp3");
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }


        YaoLrcHelper.parseUrl("http://39.106.204.190:7777/lrc/1405283464.lrc", new YaoLrcHelper.LrcCallBack() {
            @Override
            public void getLrcData(List<Lrc> list) {
                lrvView.setLrcData(list);
            }
        });

        //据播放进度更新歌词:
        lrvView.updateTime(mediaPlayer.getCurrentPosition());


        //  通过指示器，跳转至具体的时间:
        lrvView.setOnPlayIndicatorLineListener(new LrcView.OnPlayIndicatorLineListener() {
            @Override
            public void onPlay(long time, String content) {
                mediaPlayer.seekTo((int) time);
            }
        });
    }

    private void initView() {
        lrvView = (LrcView) findViewById(R.id.lrv_View);
    }
}